package com.wf.ew.orderB.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.orderB.biz.BoxSearchBiz;
import com.wf.ew.orderB.model.BoxSearch;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-05-21 09:26:45
 * @version ：1.0
 */
@Repository
public class BoxSearchBizImpl extends BaseDao implements BoxSearchBiz {

	@Override
	public String list(PageResult<T> page, BoxSearch boxSearch, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BoxSearch> rm = new BeanPropertyRowMapper<BoxSearch>(BoxSearch.class);
		StringBuffer sql = new StringBuffer("select b.id,b.companyId,b.box");
        sql.append(",b.create_time,b.create_user,b.update_time,b.update_user ");
        sql.append(" from sszg_box_search b ");
        sql.append(" where b.del=0  ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and b.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(boxSearch)){
			if(Utils.isNotEmpty(boxSearch.getBox())){
				sql.append(" and instr(b.box,:box)>0 ");
				map.put("box", boxSearch.getBox());
			}
			if(Utils.isNotEmpty(boxSearch.getSort())&&Utils.isNotEmpty(boxSearch.getOrder())){
				sql.append(" order by "+boxSearch.getSort()+" "+boxSearch.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<BoxSearch> list(BoxSearch boxSearch, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BoxSearch> rm = new BeanPropertyRowMapper<BoxSearch>(BoxSearch.class);
        StringBuffer sql = new StringBuffer("select b.id,b.companyId,b.box");
        sql.append(",b.create_time,b.create_user,b.update_time,b.update_user ");
        sql.append(" from sszg_box_search b ");
        sql.append(" where b.del=0  ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and b.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(boxSearch)){
			if(Utils.isNotEmpty(boxSearch.getBox())){
				sql.append(" and instr(b.box,:box)>0 ");
				map.put("box", boxSearch.getBox());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public int getDayCount() {
		Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select count(1) ");
        sql.append(" from sszg_box_search b ");
        sql.append(" where TO_DAYS(b.create_time) = TO_DAYS(NOW())");

		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(),map, Integer.class);
	}

	@Override
	public BoxSearch getById(BoxSearch boxSearch) {
		RowMapper<BoxSearch> rm = new BeanPropertyRowMapper<BoxSearch>(BoxSearch.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.companyId,b.box");
        sql.append(",b.create_time,b.create_user,b.update_time,b.update_user ");
        sql.append(" from sszg_box_search b ");
        sql.append(" where b.del=0  and b.id=:id ");
		map.put("id", boxSearch.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(BoxSearch boxSearch) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sszg_box_search (");
        sql.append("box) values ( ");
        sql.append(":box)");
        map.put("box", boxSearch.getBox());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(BoxSearch boxSearch, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sszg_box_search set ");
        sql.append("box=:box,update_time=now(),update_user=:update_user where id=:id ");
        map.put("box", boxSearch.getBox());
		map.put("update_user", loginUser.getId());
		map.put("id", boxSearch.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(BoxSearch boxSearch, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(boxSearch.getIds().length()>0){
			for(String ID : boxSearch.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sszg_box_search set del=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
